package com.woniuxy.depot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.depot.mapper.DepotOutStockMapper;
import com.woniuxy.depot.model.DTO.Condition;
import com.woniuxy.depot.model.DTO.OutStockDTO;
import com.woniuxy.depot.model.DTO.Statistics;
import com.woniuxy.depot.model.VO.StockGoodsVO;
import com.woniuxy.depot.model.VO.StockGoodssVO;
import com.woniuxy.depot.model.entity.DepotOutStock;
import com.woniuxy.depot.service.DepotOutStockItemService;
import com.woniuxy.depot.service.DepotOutStockService;
import com.woniuxy.util.SnowflakeIdWorker;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DepotOutStockServiceImpl implements DepotOutStockService {
    @Resource
    private DepotOutStockMapper outStockMapper;
    @Resource
    private DepotOutStockItemService itemService;
    @Override
    public Page<DepotOutStock> all(int current, int size) {
        return outStockMapper.selectPage(new Page<>(current,size),null);
    }

    @Override
    public List<DepotOutStock> searchByGoodsCode(String code) {
        List<Integer> outStockIds = itemService.getOutStockIdsByCode(code);
        QueryWrapper<DepotOutStock> wrapper = new QueryWrapper<>();
        wrapper.in("id",outStockIds);

        return outStockMapper.selectList(wrapper);
    }

    @Override
    public List<DepotOutStock> searchByCondition(Condition condition) {
        QueryWrapper<DepotOutStock> wrapper = new QueryWrapper<>();
        wrapper.eq(!("".equals(condition.getFormNo())), "form_no",condition.getFormNo()).between(!("".equals(condition.getStartTime())||"".equals(condition.getEndTime())),"create_time",condition.getStartTime(),condition.getEndTime());
        return outStockMapper.selectList(wrapper);
    }

    @Transactional
    @Override
    public Boolean updateState(String formNo, Integer state) {
        DepotOutStock outStock = new DepotOutStock().setState(state).setAuditor(this.getAccount());
        outStockMapper.update(outStock,new UpdateWrapper<DepotOutStock>().eq("form_no",formNo));
        return true;
    }

    @Transactional
    @Override
    public Boolean insert(OutStockDTO outStockDTO) {
        //添加出库单
        DepotOutStock depotOutStock = new DepotOutStock().setType(outStockDTO.getType())
                .setCustomer(outStockDTO.getCustomer()).setDepotId(outStockDTO.getDepotId())
                .setSaleId(outStockDTO.getSaleId()).setFormMaker(outStockDTO.getFormMaker())
                .setState(outStockDTO.getState()).setAbc(outStockDTO.getAbc()).setCreateTime(new Date())
                .setFormNo(String.valueOf(SnowflakeIdWorker.newId()));
        outStockMapper.insert(depotOutStock);
        //添加出库商品表
        for (int i = 0; i < outStockDTO.getItems().size(); i++) {
            outStockDTO.getItems().get(i).setOutStockId(depotOutStock.getId());
        }
        //批量新增outStockItem
        itemService.insert(outStockDTO.getItems());
        return true;
    }

    @Transactional
    @Override
    public List<StockGoodssVO> detail(Integer id) {
        List<StockGoodssVO> list = outStockMapper.detail(id);

        return list;
    }

    @Override
    public List<Statistics> statistics() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);
        String day = df.format(c.getTime());

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastday =  df.format(ca.getTime());

        return outStockMapper.queryByDate(day,lastday);
    }

    private String getAccount(){
        return  SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
    }
}
